#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   model_local_valid.py    
@Contact :   fengfeng.qiu@amh-group.com

@Modify Time      @Author    
------------      -------    
2022/2/21 10:19   qiufengfeng      
'''

import pandas as pd
import json
import requests


def post_server(data):
    url = 'http://localhost:8000/phantom/tf-predict'
    header = {
        'Content-Type':"application/json"
    }
    post_data = {
        'dialogue':[data]
    }

    req = requests.post(url,data=json.dumps(post_data),headers=header)
    return req.json()

def make_dialog_list(dialog):
    json_obj = json.loads(dialog)
    result = []
    for i in json_obj:
        result.append(i['role'] + ":"+ i["text"])
    try:
        predict_result = post_server([dialog])[0]
        if predict_result["contraband_infos"]["sure_contraband"]:
            return 1
        else:
            return 0
    except Exception as e:
        return  0

def make_dialog_list_nopredit(dialog):
    json_obj = json.loads(dialog)
    result = []
    for i in json_obj:
        result.append(i['role'] + ":"+ i["text"])
    return result


def valid_voice_text():
    file = r'D:\工作相关内容\公司项目\禁限运危化品货源\data\20220216-20220220\voice_text\20220221102917376_10563.csv'
    df = pd.read_csv(file,sep='')
    print(df)
    df["contraband_predict"] = df['asr_detail'].apply(make_dialog_list)
    df.to_csv("result.csv")

def valid_voice_text_batch():
    global model
    file = r'D:\工作相关内容\公司项目\禁限运危化品货源\data\20220216-20220220\voice_text\20220221102917376_10563.csv'
    df = pd.read_csv(file,sep='')
    df = df[:1]
    df["dialog_list"] = df['asr_detail'].apply(make_dialog_list_nopredit)




if __name__ == '__main__':
    valid_voice_text_batch()